package com.edu.d10_test;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/**
 *客户端
 */
public class Client {
    private Socket socket;
    Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws Exception {
        //创建客户端
        try {
            Client client = new Client();
            client.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void start() throws Exception {
        socket = new Socket("127.0.0.1", 8118);
        System.out.println("======客户端连接服务端成功======");
        while (true) {
            System.out.println("请您输入操作命令：");
            System.out.println("1、注册");
            System.out.println("2、登录");
            System.out.println("exit、关闭");
            System.out.println("请选择：");
            String s = scanner.next();
            switch (s){
                case "1":
                    //注册
                    register();
                    break;
                case "2":
                    //登录
                    login();
                    break;
                case "exit":
                    System.out.println("退出系统！");
                    socket.close();
                    break;
            }
        }
    }
    public void register() throws Exception {
        System.out.println("=============注册============");
        /**
         * 2、从Socket管道中，得到一个字节输出流，用来发数据给服务端程序
         */
        System.out.println("请输入注册名：");
        String account = scanner.next();
        System.out.println("请输入注册密码：");
        String password = scanner.next();
        OutputStream os =socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(os);

        //向服务端发送数据
        dataOutputStream.writeInt(1);//做个标记，代表此时发送的是注册信息
        dataOutputStream.writeUTF(account);
        dataOutputStream.writeUTF(password);
        dataOutputStream.flush();

        //接收服务端响应过来的数据
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        System.out.println(dataInputStream.readUTF());
    }

    public void login() throws Exception {
        System.out.println("=============登录============");
        /**
         * 2、从Socket管道中，得到一个字节输出流，用来发数据给服务端程序
         */
        System.out.println("请输入登录名：");
        String account = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        OutputStream os =socket.getOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(os);
        //向服务端发送数据保存
        dataOutputStream.writeInt(2);//做个标记，代表此时发送的是登录信息
        dataOutputStream.writeUTF(account);
        dataOutputStream.writeUTF(password);
        dataOutputStream.flush();

        //接收服务端响应过来的数据
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        System.out.println(dataInputStream.readUTF());
    }
}
